package com.bdzl.auth.controller;

import com.bdzl.auth.service.RoleService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.sys.RoleDO;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.sys.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统角色表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:59:22
 */

@Controller
@RequestMapping("/bdzl/role")
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;

	@GetMapping()
	String Role(){
	    return "bdzl/role/role";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<RoleDO> roleList = roleService.list(query);
		int total = roleService.count(query);
		PageUtils pageUtils = new PageUtils(roleList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "bdzl/role/add";
	}

	@GetMapping("/edit")
	@ResponseBody
	R edit(Integer roleId){
		RoleDO role = roleService.get(roleId);
	    return R.ok(role);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@Transactional
	public R save(@RequestBody RoleVO role){
		return roleService.saveRoleDO(role);
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@Transactional
	public R update(@RequestBody RoleVO role){
		return roleService.updateRoleDO(role);
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@Transactional
	public R remove(@RequestBody RoleDO roleDO){
		int remove = roleService.remove(roleDO.getRoleId());
		if(remove>0){
		return R.ok();
		}else if (remove == -10){
			return R.error("系统角色禁止删除");
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] roleIds){
		roleService.batchRemove(roleIds);
		return R.ok();
	}

	@GetMapping("/get")
	@ResponseBody
	public RoleDO getRole(Integer roleId){
		return roleService.get(roleId);
	}


	@PostMapping(value = "/getRoleById")
	@ResponseBody
	public RoleDO getRoleById(@RequestParam("roleId") Integer roleId){
		return roleService.get(roleId);
	}

	/**
	* @Description: TODO 获取所有角色数据
	* @return: java.util.List<com.bdzl.pub.entity.sys.RoleDO>
	* @Author: Ming
	* @Date: 2021/5/14 18:15
	*/
	@ResponseBody
	@GetMapping("/getRole")
	public List<RoleDO> getRole(){
		return roleService.list(new HashMap<>());
	}
}
